﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Sale_Model;
using Sale_Common;
using Sale_DAL;


namespace Sale_Operation
{
    public class ClientBackBillOp : Component
    {
        public override bool Save(object obj)
        {
            bool blRes = false;
            ClientBackBill SM = obj as ClientBackBill;
            int ioldId = SM.CBB_Id;
            db = new DataBase();
            try
            {
                StringBuilder sbSql = new StringBuilder();

                sbSql.Append("UPDATE ClientBackBill SET ");
                sbSql.Append("CBB_Code='" + ValueHandler.GetStringValue(SM.CBB_Code, ValueHandler.CharHandlerType.IsRepChar) + "',");
                sbSql.Append("CBB_SBill_Id='" + ValueHandler.GetStringValue(SM.CBB_SBill_Id, ValueHandler.CharHandlerType.IsRepChar) + "',");
                sbSql.Append("CBB_Date='" + ValueHandler.GetStringValue(SM.CBB_Date, ValueHandler.CharHandlerType.IsRepChar) + "',");
                sbSql.Append("CBB_Reason='" + ValueHandler.GetStringValue(SM.CBB_Reason, ValueHandler.CharHandlerType.IsRepChar) + "',");
                sbSql.Append("CBB_Money='" + ValueHandler.GetStringValue(SM.CBB_Money, ValueHandler.CharHandlerType.IsRepChar) + "',");
                sbSql.Append("CBB_SUser_Id='" + ValueHandler.GetStringValue(SM.CBB_SUser_Id, ValueHandler.CharHandlerType.IsRepChar) + "',");
                sbSql.Append("CBB_Input='" + ValueHandler.GetStringValue(SM.CBB_Input, ValueHandler.CharHandlerType.IsRepChar) + "',");
                sbSql.Append("CBB_State='" + ValueHandler.GetStringValue(SM.CBB_State, ValueHandler.CharHandlerType.IsRepChar) + "',");
                sbSql.Append("CBB_PayType='" + ValueHandler.GetStringValue(SM.CBB_PayType, ValueHandler.CharHandlerType.IsRepChar) + "',");
                sbSql.Append("CBB_AList_Id='" + ValueHandler.GetStringValue(SM.CBB_AList_Id, ValueHandler.CharHandlerType.IsRepChar) + "' ");
                sbSql.Append("WHERE CBB_Id=" + SM.CBB_Id);
                db.Open();
                SqlHelper.ExecuteNonQuery(db.Connection, CommandType.Text, sbSql.ToString());
                blRes = true;
            }
            catch (Exception ex)
            {                
                LinkService.InsertLog(ESysLogType.Error, "新增/修改退货单信息出错！[" + ex.ToString() + "]", EMenuList.退货管理);
            }
            db.Close();
            if (ioldId == 0)
            {
                LinkService.InsertLog(ESysLogType.Operation, "新增退货单信息[" + ValueHandler.GetStringValue(SM.CBB_Code, ValueHandler.CharHandlerType.IsRepChar) + "]！", EMenuList.退货管理);
            }
            else
            {
                LinkService.InsertLog(ESysLogType.Operation, "修改退货单信息[" + ValueHandler.GetStringValue(SM.CBB_Code, ValueHandler.CharHandlerType.IsRepChar) + "]！", EMenuList.退货管理);
            }
            db.Close();
            return blRes;
        }

        public override bool HasExists(object obj)
        {
            ClientBackBill SM = obj as ClientBackBill;
            bool blRes = true;
            try
            {
                db = new DataBase();
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(" SELECT * FROM ClientBackBill WHERE CBB_Code='" + SM.CBB_Code + "' AND CBB_Id<>" + SM.CBB_Id + " AND State=1 ");
                SqlHelper.FillDataset(db.Connection, CommandType.Text, sbSql.ToString(), db.Data, new string[] { "dtList" });
                if (db.Data.Tables[0].Rows.Count > 0)
                    blRes = true;
                else
                    blRes = false;

            }
            catch (Exception ex)
            {
            }
            db.Close();
            return blRes;
        }

        public override bool Delete(object obj)
        {
            DataBase db = new DataBase();
            bool blRes = false;
            try
            {
                db.Open();
                ClientBackBill SM = obj as ClientBackBill;
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(" UPDATE ClientBackBill SET ");
                sbSql.Append(" State=0 ");
                sbSql.Append(" WHERE CBB_Id=" + SM.CBB_Id);
                sbSql.Append(" UPDATE ClientBackBillDetail SET ");
                sbSql.Append(" State=0 ");
                sbSql.Append(" WHERE CBBDeta_CBB_Id=" + SM.CBB_Id);
                SqlHelper.ExecuteNonQuery(db.Connection, CommandType.Text, sbSql.ToString());
                blRes = true;
                LinkService.InsertLog(ESysLogType.Operation, "删除退货单信息[" + ValueHandler.GetStringValue(SM.CBB_Code, ValueHandler.CharHandlerType.IsRepChar) + "]！", EMenuList.退货管理);
            }
            catch (Exception ex)
            {
                LinkService.InsertLog(ESysLogType.Error, "删除退货单信息失败！[" + ex.ToString() + "]", EMenuList.退货管理);
            }
            db.Close();

            return blRes; ;
        }

        public override DataTable GetList(string strCondition, int iCurrentPageIndex, ref int iRecordCount)
        {
            try
            {
                db = new DataBase();
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(" SELECT CBB_Id,CBB_Code,CBB_SBill_Id,CBB_Date,CBB_Reason,CBB_Money,CBB_Input,CBB_State,CBB_PayType,CBB_AList_Id,SBill_Code,SBill_Code2, C.Cust_Name,D.SUser_Name ,A.CBB_ARecoId FROM ClientBackBill A ");
                sbSql.Append(" LEFT JOIN SaleBill B ON (A.CBB_SBill_Id=B.SBill_Id)");
                sbSql.Append(" LEFT JOIN Customer C ON (C.Cust_Id=B.SBill_Cust_Id)");
                sbSql.Append(" LEFT JOIN SysUser D ON (A.CBB_SUser_Id=D.SUser_Id)");
                sbSql.Append(" WHERE A.State=1 ");
                if (!string.IsNullOrEmpty(strCondition))
                {
                    sbSql.Append(" AND " + strCondition);
                }
                sbSql.Append(" ORDER BY A.CreateTime DESC ");
                db.Data = SqlHelper.FillDataSet(db.Connection, sbSql.ToString(), iCurrentPageIndex, ref iRecordCount, LinkService.PageSize);
            }
            catch (Exception ex)
            {
                LinkService.InsertLog(ESysLogType.Error, "退货列表加载失败！[" + ex.ToString() + "]", EMenuList.退货管理);
            }
            db.Close();
            return db.Data.Tables[0];
        }
        /// <summary>
        /// 获取某个退货单明细信息
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public System.Data.DataTable GetDetail(object obj)
        {
            db = new DataBase();
            try
            {
                ClientBackBill SM = obj as ClientBackBill;
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(" SELECT CBB_Id,CBB_Code,CBB_SBill_Id,CBB_Date,CBB_SUser_Id,CBB_Reason,CBB_Money,CBB_Input,CBB_State,CBB_PayType,CBB_AList_Id,SBill_Code,SBill_Code2, C.Cust_Name,D.SUser_Name ,A.CreateMan,A.CreateTime,A.State,A.CBB_ARecoId FROM ClientBackBill A ");
                sbSql.Append(" LEFT JOIN SaleBill B ON (A.CBB_SBill_Id=B.SBill_Id)");
                sbSql.Append(" LEFT JOIN Customer C ON (C.Cust_Id=B.SBill_Cust_Id)");
                sbSql.Append(" LEFT JOIN SysUser D ON (A.CBB_SUser_Id=D.SUser_Id)");
                sbSql.Append(" WHERE A.State=1 AND CBB_Id=" + SM.CBB_Id);
                SqlHelper.FillDataset(db.Connection, CommandType.Text, sbSql.ToString(), db.Data, new string[] { "dtList" });
                if (db.Data.Tables[0].Rows.Count > 0)
                {
                    DataRow dr = db.Data.Tables[0].Rows[0];
                    SM.CBB_AList_Id = ValueHandler.GetIntNumberValue(dr["CBB_AList_Id"]);
                    SM.CBB_Code = ValueHandler.GetStringValue(dr["CBB_Code"]);
                    SM.CBB_Date = DateTime.Parse(dr["CBB_Date"].ToString());
                    SM.CBB_Id = ValueHandler.GetIntNumberValue(dr["CBB_Id"]);
                    SM.CBB_Input = ValueHandler.GetIntNumberValue(dr["CBB_Input"]);
                    SM.CBB_Money = ValueHandler.GetDecNumberValue(dr["CBB_Money"]);
                    SM.CBB_PayType = ValueHandler.GetIntNumberValue(dr["CBB_PayType"]);
                    SM.CBB_Reason = ValueHandler.GetStringValue(dr["CBB_Reason"]);
                    SM.CBB_SBill_Id = ValueHandler.GetIntNumberValue(dr["CBB_SBill_Id"]);
                    SM.CBB_State = ValueHandler.GetIntNumberValue(dr["CBB_State"]);
                    SM.CBB_SUser_Id = ValueHandler.GetIntNumberValue(dr["CBB_SUser_Id"]);
                    SM.CreateMan = ValueHandler.GetIntNumberValue(dr["CreateMan"]);
                    SM.CreateTime = DateTime.Parse(dr["CreateTime"].ToString());
                    SM.State = ValueHandler.GetIntNumberValue(dr["State"]);
                }
                db.Close();
                db = new DataBase();
                sbSql = new StringBuilder();
                sbSql.Append(" SELECT CBBDeta_Id  ,CBBDeta_CBB_Id,CBBDeta_PInfo_Id,CBBDeta_Count,CBBDeta_Price,CBBDeta_Money,CBBDeta_Memo ,PInfo_Code,PInfo_Name,PInfo_PSort_ID ,C.PSort_Name FROM ClientBackBillDetail A ");
                sbSql.Append(" LEFT JOIN ProductInfo B ON (A.CBBDeta_PInfo_Id=B.PInfo_Id) ");
                sbSql.Append(" LEFT JOIN ProductSort C ON (B.PInfo_PSort_ID=C.PSort_ID) ");
                sbSql.Append(" WHERE A.CBBDeta_CBB_Id=" + SM.CBB_Id + " AND A.State=1");
                SqlHelper.FillDataset(db.Connection, CommandType.Text, sbSql.ToString(), db.Data, new string[] { "dtList" });
                db.Data.Tables[0].Columns["CBBDeta_Id"].AutoIncrement = true;
                db.Data.Tables[0].Columns["CBBDeta_Id"].AutoIncrementStep = 1;
                if (db.Data.Tables[0].Rows.Count <= 0)
                    db.Data.Tables[0].Columns["CBBDeta_Id"].AutoIncrementSeed = 1;
                else
                    db.Data.Tables[0].Columns["CBBDeta_Id"].AutoIncrementSeed = ValueHandler.GetIntNumberValue(db.Data.Tables[0].Compute("max(CBBDeta_Id)+1", ""));

            }
            catch (Exception ex)
            {
                LinkService.InsertLog(ESysLogType.Error, "退货列表加载失败！[" + ex.ToString() + "]", EMenuList.退货管理);
            }
            db.Close();
            return db.Data.Tables[0];
        }
        public bool Save(object obj, DataTable dtDetail)
        {
            bool blRes = false;
            ClientBackBill SM = obj as ClientBackBill;
            int ioldId = SM.CBB_Id;
            db = new DataBase();
            try
            {
                StringBuilder sbSql = new StringBuilder();
                if (SM.CBB_Id == 0)
                {
                    sbSql.Append("INSERT INTO ClientBackBill (CBB_Code,CBB_SBill_Id,CBB_Date,CBB_Reason,CBB_Money,CBB_SUser_Id,CBB_Input,CBB_State,CBB_PayType,CBB_AList_Id,State,CreateTime,CreateMan) SELECT ");
                    sbSql.Append("CBB_Code='" + ValueHandler.GetStringValue(SM.CBB_Code, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("CBB_SBill_Id='" + ValueHandler.GetStringValue(SM.CBB_SBill_Id, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("CBB_Date='" + ValueHandler.GetStringValue(SM.CBB_Date, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("CBB_Reason='" + ValueHandler.GetStringValue(SM.CBB_Reason, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("CBB_Money='" + ValueHandler.GetStringValue(SM.CBB_Money, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("CBB_SUser_Id='" + ValueHandler.GetStringValue(SM.CBB_SUser_Id, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("CBB_Input='" + ValueHandler.GetStringValue(SM.CBB_Input, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("CBB_State='" + ValueHandler.GetStringValue(SM.CBB_State, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("CBB_PayType='" + ValueHandler.GetStringValue(SM.CBB_PayType, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("CBB_AList_Id='" + ValueHandler.GetStringValue(SM.CBB_AList_Id, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("State=1,");
                    sbSql.Append("CreateTime=getdate(),");
                    sbSql.Append("CreateMan=" + LinkService.GetCurrentUser().SUser_Id);
                    db.Open();
                    SqlHelper.ExecuteNonQuery(db.Transaction, CommandType.Text, sbSql.ToString());
                    SM.CBB_Id = ValueHandler.GetIntNumberValue(SqlHelper.ExecuteScalar(db.Transaction, CommandType.Text, "select @@identity "));
                }
                else
                {
                    sbSql.Append("UPDATE ClientBackBill SET ");
                    sbSql.Append("CBB_Code='" + ValueHandler.GetStringValue(SM.CBB_Code, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("CBB_SBill_Id='" + ValueHandler.GetStringValue(SM.CBB_SBill_Id, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("CBB_Date='" + ValueHandler.GetStringValue(SM.CBB_Date, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("CBB_Reason='" + ValueHandler.GetStringValue(SM.CBB_Reason, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("CBB_Money='" + ValueHandler.GetStringValue(SM.CBB_Money, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("CBB_SUser_Id='" + ValueHandler.GetStringValue(SM.CBB_SUser_Id, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("CBB_Input='" + ValueHandler.GetStringValue(SM.CBB_Input, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("CBB_State='" + ValueHandler.GetStringValue(SM.CBB_State, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("CBB_PayType='" + ValueHandler.GetStringValue(SM.CBB_PayType, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("CBB_AList_Id='" + ValueHandler.GetStringValue(SM.CBB_AList_Id, ValueHandler.CharHandlerType.IsRepChar) + "' ");
                    sbSql.Append("WHERE CBB_Id=" + SM.CBB_Id);
                    db.Open();
                    SqlHelper.ExecuteNonQuery(db.Transaction, CommandType.Text, sbSql.ToString());
                }
                //新增明细信息
                sbSql = new StringBuilder();
                sbSql.Append(" DELETE FROM ClientBackBillDetail WHERE CBBDeta_CBB_Id=" + SM.CBB_Id);
                SqlHelper.ExecuteNonQuery(db.Transaction, CommandType.Text, sbSql.ToString());
                for (int i = 0; i < dtDetail.Rows.Count; i++)
                {
                    if (dtDetail.Rows[i].RowState == DataRowState.Deleted)
                        continue;
                    sbSql = new StringBuilder();
                    sbSql.Append(" INSERT INTO ClientBackBillDetail(CBBDeta_CBB_Id,CBBDeta_PInfo_Id,CBBDeta_Count,CBBDeta_Price,CBBDeta_Money,CBBDeta_Memo,State,CreateTime,CreateMan) SELECT ");
                    sbSql.Append(" CBBDeta_CBB_Id=" + SM.CBB_Id + ",");
                    sbSql.Append(" CBBDeta_PInfo_Id=" + ValueHandler.GetStringValue(dtDetail.Rows[i]["CBBDeta_PInfo_Id"]) + ",");
                    sbSql.Append(" CBBDeta_Count=" + ValueHandler.GetStringValue(dtDetail.Rows[i]["CBBDeta_Count"]) + ",");
                    sbSql.Append(" CBBDeta_Price=" + ValueHandler.GetStringValue(dtDetail.Rows[i]["CBBDeta_Price"]).ToString() + ",");
                    sbSql.Append(" CBBDeta_Money=" + ValueHandler.GetStringValue(dtDetail.Rows[i]["CBBDeta_Money"]).ToString() + ",");
                    sbSql.Append(" CBBDeta_Memo='" + ValueHandler.GetStringValue(dtDetail.Rows[i]["CBBDeta_Memo"], ValueHandler.CharHandlerType.IsRepChar).ToString() + "',");
                    sbSql.Append(" State=1,");
                    sbSql.Append(" CreateTime=getdate(),");
                    sbSql.Append(" CreateMan=" + SM.CreateMan);
                    SqlHelper.ExecuteNonQuery(db.Transaction, CommandType.Text, sbSql.ToString());
                }
                db.Transaction.Commit();
                blRes = true;
            }
            catch (Exception ex)
            {
                db.Transaction.Rollback();
                LinkService.InsertLog(ESysLogType.Error, "新增/修改退货单信息出错！[" + ex.ToString() + "]", EMenuList.退货管理);
            }
            db.Close();
            if (ioldId == 0)
            {
                LinkService.InsertLog(ESysLogType.Operation, "新增退货单信息[" + ValueHandler.GetStringValue(SM.CBB_Code, ValueHandler.CharHandlerType.IsRepChar) + "]！", EMenuList.退货管理);
            }
            else
            {
                LinkService.InsertLog(ESysLogType.Operation, "修改退货单信息[" + ValueHandler.GetStringValue(SM.CBB_Code, ValueHandler.CharHandlerType.IsRepChar) + "]！", EMenuList.退货管理);
            } 
            return blRes;
        }
        public bool JS(object obj)
        {

            ClientBackBill SModel = obj as ClientBackBill;
            AccountRecord SM = new AccountRecord();
            SM.AReco_Code = Coder.CreateCoder(EMenuList.转账管理);
            SM.AReco_Datetime = DateTime.Parse(System.DateTime.Now.ToString("yyyy-MM-dd"));
            SM.AReco_InNumber = -1;
            SM.AReco_Kind = 2;
            SM.AReco_Man = LinkService.GetCurrentUser().SUser_Id;
            SM.AReco_Noney = SModel.CBB_Money;
            SM.AReco_OutNumber = SModel.CBB_AList_Id;
            SM.AReco_Reason = "退货单[" + SModel.CBB_Code + "]结算，结算金额：" + SModel.CBB_Money;
            SM.AReco_Type = 2;
            SM.CreateMan = LinkService.GetCurrentUser().SUser_Id;
            SM.CreateTime = System.DateTime.Now;
            SM.State = 1;
            AccountRecordOp accountRecordManager = new AccountRecordOp();
            bool blRes = accountRecordManager.Save(SM);
            if (blRes)
            {
                try
                {
                    StringBuilder sbSql = new StringBuilder();
                    sbSql.Append(" UPDATE ClientBackBill SET CBB_State = 1 , CBB_ARecoId=" + SM.ARecoId + " WHERE CBB_Id=" + SModel.CBB_Id);
                    db.Open();
                    SqlHelper.ExecuteNonQuery(db.Connection, CommandType.Text, sbSql.ToString());
                    blRes = true;
                    LinkService.InsertLog(ESysLogType.Operation, "退货单[" + SModel.CBB_Code + "]结算完成！", EMenuList.退货管理);
                }
                catch (Exception ex)
                {
                    blRes = false;
                }
                db.Close();
            }
            return blRes;
        }
        public override object GetModel(object obj)
        {
            db = new DataBase();
            ClientBackBill SM = obj as ClientBackBill;

            try
            {
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(" SELECT CBB_Id,CBB_Code,CBB_SBill_Id,CBB_Date,CBB_Reason,CBB_Money,CBB_Input,CBB_State,CBB_PayType,CBB_AList_Id,SBill_Code,SBill_Code2, C.Cust_Name,D.SUser_Name ,A.CBB_ARecoId,CBB_SUser_Id,A.CreateMan,A.CreateTime,A.State FROM ClientBackBill A ");
                sbSql.Append(" LEFT JOIN SaleBill B ON (A.CBB_SBill_Id=B.SBill_Id)");
                sbSql.Append(" LEFT JOIN Customer C ON (C.Cust_Id=B.SBill_Cust_Id)");
                sbSql.Append(" LEFT JOIN SysUser D ON (A.CBB_SUser_Id=D.SUser_Id)");
                sbSql.Append(" WHERE A.State=1 AND CBB_Id=" + SM.CBB_Id);
                SqlHelper.FillDataset(db.Connection, CommandType.Text, sbSql.ToString(), db.Data, new string[] { "dtList" });
                if (db.Data.Tables[0].Rows.Count > 0)
                {
                    DataRow dr = db.Data.Tables[0].Rows[0];
                    SM.CBB_AList_Id = ValueHandler.GetIntNumberValue(dr["CBB_AList_Id"]);
                    SM.CBB_Code = ValueHandler.GetStringValue(dr["CBB_Code"]);
                    SM.CBB_Date = DateTime.Parse(dr["CBB_Date"].ToString());
                    SM.CBB_Id = ValueHandler.GetIntNumberValue(dr["CBB_Id"]);
                    SM.CBB_Input = ValueHandler.GetIntNumberValue(dr["CBB_Input"]);
                    SM.CBB_Money = ValueHandler.GetDecNumberValue(dr["CBB_Money"]);
                    SM.CBB_PayType = ValueHandler.GetIntNumberValue(dr["CBB_PayType"]);
                    SM.CBB_Reason = ValueHandler.GetStringValue(dr["CBB_Reason"]);
                    SM.CBB_SBill_Id = ValueHandler.GetIntNumberValue(dr["CBB_SBill_Id"]);
                    SM.CBB_State = ValueHandler.GetIntNumberValue(dr["CBB_State"]);
                    SM.CBB_SUser_Id = ValueHandler.GetIntNumberValue(dr["CBB_SUser_Id"]);
                    SM.CreateMan = ValueHandler.GetIntNumberValue(dr["CreateMan"]);
                    SM.CreateTime = DateTime.Parse(dr["CreateTime"].ToString());
                    SM.State = ValueHandler.GetIntNumberValue(dr["State"]);
                    SM.CBB_ARecoId = ValueHandler.GetIntNumberValue(dr["CBB_ARecoId"]);
                }
            }
            catch (Exception ex)
            {
                LinkService.InsertLog(ESysLogType.Error, "退货列表加载失败！[" + ex.ToString() + "]", EMenuList.退货管理);
            }
            db.Close();
            return SM;
        }
        /// <summary>
        /// 销售退货单产品
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool PutIn(ClientBackBill obj)
        {
            //获取明细信息
            bool blRes = false;
            StringBuilder sbSql = new StringBuilder();
            ClientBackBill bb = obj as ClientBackBill;
            db = new DataBase();
            try
            {
                DataTable dtList = GetDetail(obj);
                db.Open();
                for (int i = 0; i < dtList.Rows.Count; i++)
                {
                    sbSql = new StringBuilder();
                    string strSDeta_Stock_Id = "1";//仓库默认为1
                    int iSDeta_PInfo_Id = ValueHandler.GetIntNumberValue(dtList.Rows[i]["CBBDeta_PInfo_Id"]);
                    int iBBDeta_Count = ValueHandler.GetIntNumberValue(dtList.Rows[i]["CBBDeta_Count"]);

                    sbSql.Append(@"  IF EXISTS(SELECT * FROM StockDetail WHERE SDeta_PInfo_Id=" + iSDeta_PInfo_Id.ToString() + @")
                                BEGIN
	                                UPDATE StockDetail SET SDeta_RealNumber=SDeta_RealNumber+" + iBBDeta_Count.ToString() + @",SDeta_OutputNumber=SDeta_OutputNumber-" + iBBDeta_Count.ToString() + @" WHERE SDeta_PInfo_Id=" + iSDeta_PInfo_Id.ToString() + @"
                                END
                                ELSE
                                BEGIN
	                                INSERT INTO StockDetail (SDeta_Stock_Id,SDeta_PInfo_Id,SDeta_InputNumber,SDeta_OutputNumber,SDeta_RealNumber,State,CreateTime,CreateMan)
                                SELECT SDeta_Stock_Id=" + strSDeta_Stock_Id + @",
                                SDeta_PInfo_Id=" + iSDeta_PInfo_Id.ToString() + @",
                                SDeta_InputNumber=" + iBBDeta_Count.ToString() + @",
                                SDeta_OutputNumber=0,
                                SDeta_RealNumber=" + iBBDeta_Count.ToString() + @",
                                State=1,
                                CreateTime=GETDATE(),
                                CreateMan=1
                                END ");
                    SqlHelper.ExecuteNonQuery(db.Transaction, CommandType.Text, sbSql.ToString());

                }
                //修改退货单主表状态
                SqlHelper.ExecuteNonQuery(db.Transaction, CommandType.Text, "UPDATE ClientBackBill SET CBB_Input=1 WHERE CBB_Id=" + bb.CBB_Id.ToString());
                db.Transaction.Commit();
                blRes = true;
                LinkService.InsertLog(ESysLogType.Operation, "退货单[" + bb.CBB_Code + "]入库完成！", EMenuList.退货管理);
            }
            catch (Exception ex)
            {
                db.Transaction.Rollback();
                LinkService.InsertLog(ESysLogType.Operation, "退货单[" + bb.CBB_Code + "]入库失败！" + ex.ToString(), EMenuList.退货管理);

            }
            db.Close();
            return blRes;
        }
    }
}
